package com.surrey.p2p.model;

import java.text.DecimalFormat;

public class Point {
	
	double x;
	double y;
	
	public Point (double x,double y) throws Exception{
		if(x>=0 && x<=1 && y>=0 && y<=1){
		this.x=x;
		this.y=y;
		}
		else{
			throw new Exception("Invalid Point for network, values should be between 0 and 1.\n( " + x+ " , " +y+")" );
		}
	}

	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(double y) {
		this.y = y;
	}
	
	public double distance(Point other){
		double xDistance=this.x-other.getX();
		double yDistance=this.y-other.getY();
		if(xDistance>0.5){
			xDistance=1-xDistance;
		}
		if(yDistance>0.5){
			yDistance=1-yDistance;
		}
		return Math.sqrt(Math.pow(xDistance,2) + Math.pow(yDistance, 2));
	}

	@Override
	public String toString() {
		DecimalFormat d=new DecimalFormat("#.000");
		return "("+ d.format(x) + " ,  " + d.format(y) + ")";
	}
	

}
